package jp.co.toshiba.ffsa.action.csas;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.annotation.Resource;

import jp.co.toshiba.ffsa.action.common.CommonAction;
import jp.co.toshiba.ffsa.common.ListBox;
import jp.co.toshiba.ffsa.dto.LoginDto;
import jp.co.toshiba.ffsa.entity.User;
import jp.co.toshiba.ffsa.form.csas.Csas201Form;
import jp.co.toshiba.ffsa.service.common.ConstantValue;
import jp.co.toshiba.ffsa.service.csas.Csas201Service;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ActionMessagesUtil;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.RequestUtil;

/**
 * 登録情報変更
 * @author npc
 */
public class Csas201Action extends CommonAction {

    /** 画面ID */
    public String screenID = "CSAS201";

    /**
     * Regionリストボックス作成用
     */
    public LinkedHashMap<String, String> regionList=new LinkedHashMap<String, String>();

    /** ActionForm */
    @ActionForm
    @Resource
    protected Csas201Form csas201Form;

    public String checkFlag;

    /**
     * 初期処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = false)
    public String index() throws Exception {
        // ログイン済判定する
        if(this.judgeLogin(csas201Form.getSessionId())){
            try {
                // リファラ判定
                if(this.judgeRefferer()){
                    // リストボックスの作成
                    ListBox listbox = new ListBox();
                    regionList=listbox.getUpdRegionListBox();

                    // Serviceクラスを呼び出す
                    Csas201Service service = new Csas201Service();
                    service.setUserId(((LoginDto)RequestUtil.getRequest().getSession().getAttribute("login_dto")).getUserID());
                    User getUser = service.getUser();

                    // ユーザーが存在した場合
                    if(getUser != null){
                        // 初期の時だけセット
                        if(this.csas201Form.returnFlag == null || this.csas201Form.returnFlag.equals("")){
                            this.csas201Form.emailAddress = getUser.mailAddress;
                            this.csas201Form.firstName    = getUser.firstName;
                            this.csas201Form.lastName     = getUser.lastName;
                            this.csas201Form.company      = getUser.company;
                            this.csas201Form.department   = getUser.department;
                            this.csas201Form.phone        = getUser.phone;
                            this.csas201Form.region       = getUser.region;
                            this.csas201Form.country      = getUser.country;
                            this.csas201Form.city         = getUser.city;
                        }
                    }else{
                        // ユーザーが存在しない場合
                        ActionMessages messages = new ActionMessages();
                        messages.add("userHaveExisted", new ActionMessage("errors.exist",MessageResourcesUtil.getMessage("mail_address"),true));
                        ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                    }

                    return "/csas/csas201.jsp";
                }else{
                    return this.topTransition;
                }
            } catch (Exception e) {
                this.errorLogWriter(e, "index", "CSAS201");
                throw e;
            }
        }else{
            return this.topTransition;
        }
    }

    /**
     * 確認処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = true,input="/csas/csas201")
    public String check() throws Exception {
        String transition = "/csas/csas201";
        String result = "";
        // ログイン済判定する
        if(this.judgeLogin(csas201Form.getSessionId())){
            try {
                // リファラ判定
                if(this.judgeRefferer()){
                    transition = "/csas/csas202.jsp";
                    result = ConstantValue.success;
                    // 操作ログ記述
                    ArrayList<ArrayList<String>> parameters = new ArrayList<ArrayList<String>>();
                    ArrayList<String> parameterNames = new ArrayList<String>();
                    ArrayList<String> values = new ArrayList<String>();
                    parameterNames.add(MessageResourcesUtil.getMessage("first_name"));
                    values.add(this.csas201Form.firstName);
                    parameterNames.add(MessageResourcesUtil.getMessage("last_name"));
                    values.add(this.csas201Form.lastName);
                    parameterNames.add(MessageResourcesUtil.getMessage("company"));
                    values.add(this.csas201Form.company);
                    parameterNames.add(MessageResourcesUtil.getMessage("department"));
                    values.add(this.csas201Form.department);
                    parameterNames.add(MessageResourcesUtil.getMessage("phone"));
                    values.add(this.csas201Form.phone);
                    parameterNames.add(MessageResourcesUtil.getMessage("region"));
                    values.add(this.csas201Form.region);
                    parameterNames.add(MessageResourcesUtil.getMessage("country"));
                    values.add(this.csas201Form.country);
                    parameterNames.add(MessageResourcesUtil.getMessage("city"));
                    values.add(this.csas201Form.city);
                    parameters.add(parameterNames);
                    parameters.add(values);
                    this.entryOperationHistory(this.screenID, "submit", parameters, result);
                }else{
                    return this.topTransition;
                }
            }catch(Exception e) {
                this.errorLogWriter(e, "check", "CSAS201");
                throw e;
            }
        }else{
            return this.topTransition;
        }
        return transition;
    }
}